/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws_client;

import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.*;

/**
 *
 * @author black
 */
public class WSAccessor
{
    protected QName serviceName;
    protected String endpoint;
    protected String namespace;

    public WSAccessor()
    {
    }

    protected Object callOperation(
        String operation,
        ParameterSpecification[] ps,
        Object[] args,
        QName returnType)
            throws ServiceException, RemoteException
    {
        // Create Service and then create a Call
        ServiceFactory factory = ServiceFactory.newInstance();
        Service service = factory.createService(serviceName);
        Call call = service.createCall();

        // create operation name to call the WS
        QName operationName = new QName(
            namespace,
            operation);

        call.setOperationName(operationName);

        // for each param in ps call
        // addParameter(param_name, XML type QName, Java Type, mode)
        for (ParameterSpecification p : ps)
            call.addParameter(
                p.getName(),
                p.getType(),
                p.getParamClass(),
                p.getMode());

        if (returnType != null)
            call.setReturnType(returnType);

        call.setProperty(Call.OPERATION_STYLE_PROPERTY, "rpc");
        call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, "");

        call.setTargetEndpointAddress(endpoint);

        Object ret = call.invoke(args);

        return ret;
    }
}
